package com.abso.mp3tunes.locker.ui.actions;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.abso.mp3tunes.locker.core.data.Album;
import com.abso.mp3tunes.locker.ui.LockerImages;
import com.abso.mp3tunes.locker.ui.viewers.LockerViewer;

/**
 * Action able to open the Last.fm page associated with a specific album.
 */
public class OpenLastFMAlbumPageAction extends AbstractOpenBrowserAction {

	/** The selected album. */
	private Album album;

	/**
	 * Constructs a new action.
	 * 
	 * @param lockerViewer
	 *            the Locker tree viewer.
	 */
	public OpenLastFMAlbumPageAction(LockerViewer lockerViewer) {
		super("Open Last.fm Page", LockerImages.getDescriptor(LockerImages.LAST_FM),
				lockerViewer);
	}

	/**
	 * Sets the current album.
	 * 
	 * @param album
	 *            the selected album.
	 */
	public void setSelectedAlbum(Album album) {
		this.album = album;
	}

	/* (non-Javadoc)
	 * @see com.abso.mp3tunes.locker.ui.actions.AbstractOpenBrowserAction#getURL()
	 */
	protected String getURL() {
		try {
			return "http://www.last.fm/music/" + URLEncoder.encode(album.getArtistName(), "UTF-8") + '/' + URLEncoder.encode(album.getTitle(), "UTF-8") ;
		} catch (UnsupportedEncodingException e) {
			return "http://www.last.fm/music/" + album.getArtistName() + '/' + album.getTitle();
		}
	}

}
